﻿namespace Helper
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Web;

    public class HttpHelper
    {
        private CookieContainer _cc = new CookieContainer();
        private int _delayTime;
        private string _lastUrl = string.Empty;
        private WebProxy _proxy;
        private int _timeout = 0x1d4c0;
        private int _tryTimes = 3;
        private string reqUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; InfoPath.2)";

        public void Download(string url, string localfile)
        {
            new WebClient().DownloadFile(url, localfile);
        }

        public static string EncodePostData(string data)
        {
            return HttpUtility.UrlEncode(data);
        }

        public string Get(string url)
        {
            string str = this.Get(url, this._lastUrl);
            this._lastUrl = url;
            return str;
        }

        public string Get(string url, string referer)
        {
            int num = this._tryTimes;
            while (num-- > 0)
            {
                try
                {
                    if (this._delayTime > 0)
                    {
                        Thread.Sleep((int) (this._delayTime * 0x3e8));
                    }
                    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri(url));
                    request.UserAgent = this.reqUserAgent;
                    request.CookieContainer = this._cc;
                    request.Referer = referer;
                    request.Method = "GET";
                    request.Timeout = this._timeout;
                    if ((this._proxy != null) && (this._proxy.Credentials != null))
                    {
                        request.UseDefaultCredentials = true;
                    }
                    request.Proxy = this._proxy;
                    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    return reader.ReadToEnd();
                }
                catch (Exception exception)
                {
                    TraceLog.Error("HTTP GET Error: " + exception.Message, new object[0]);
                    TraceLog.Error("Url: " + url, new object[0]);
                    continue;
                }
            }
            return string.Empty;
        }

        public string Post(string url, string content)
        {
            string str = this.Post(url, content, this._lastUrl);
            this._lastUrl = url;
            return str;
        }

        public string Post(string url, string content, string referer)
        {
            int num = this._tryTimes;
            while (num-- > 0)
            {
                try
                {
                    if (this._delayTime > 0)
                    {
                        Thread.Sleep((int) (this._delayTime * 0x3e8));
                    }
                    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri(url));
                    request.UserAgent = this.reqUserAgent;
                    request.CookieContainer = this._cc;
                    request.Referer = referer;
                    byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(content);
                    request.Method = "POST";
                    request.Timeout = this._timeout;
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = bytes.Length;
                    if ((this._proxy != null) && (this._proxy.Credentials != null))
                    {
                        request.UseDefaultCredentials = true;
                    }
                    request.Proxy = this._proxy;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    return reader.ReadToEnd();
                }
                catch (Exception exception)
                {
                    TraceLog.Error("HTTP POST Error: " + exception.Message, new object[0]);
                    TraceLog.Error("Url: " + url, new object[0]);
                    TraceLog.Error("Data: " + content, new object[0]);
                    continue;
                }
            }
            return string.Empty;
        }

        public void SetConfig(GlobalConfigData globalConfigData)
        {
            if (globalConfigData != null)
            {
                this.SetProxy(globalConfigData.ProxyServer, globalConfigData.ProxyPort, globalConfigData.ProxyUser, globalConfigData.ProxyPass);
                this.SetDelayConnect(globalConfigData.ConnectDelayTime);
                this.SetTimeOut(globalConfigData.HttpTimeout);
                this.SetTryTimes(globalConfigData.HttpTryTimes);
            }
        }

        public void SetDelayConnect(int delayTime)
        {
            this._delayTime = delayTime;
        }

        public void SetProxy(string server, int port, string username, string password)
        {
            if ((server != null) && (port > 0))
            {
                this._proxy = new WebProxy(server, port);
                if ((username != null) && (password != null))
                {
                    this._proxy.Credentials = new NetworkCredential(username, password);
                    this._proxy.BypassProxyOnLocal = true;
                }
            }
        }

        public void SetTimeOut(int timeout)
        {
            if (timeout > 0)
            {
                this._timeout = timeout;
            }
        }

        public void SetTryTimes(int times)
        {
            if (times > 0)
            {
                this._tryTimes = times;
            }
        }
    }
}

